#include "libUnitTest/UnitTestPP.hpp"
#include "log.hpp"
#include <thread>
#include <mutex>
#include <chrono>
#include <stdexcept>
#include <assert.h>

using namespace UnitTest;

namespace {
    int counter = 0;
    std::mutex mtx;
    void increase(int time) {
        for (int i = 0; i < time; i++) {
            // 当前线程休眠1毫秒
            mtx.lock();
            std::this_thread::sleep_for(std::chrono::milliseconds(1));
            counter++;
            mtx.unlock();
        }
    }

    TEST(multiThreadTestMutex)
    {
        std::thread t1(increase, 10000);
        std::thread t2(increase, 10000);
        t1.join();
        t2.join();
        log(LOG_TRACE, std::format("counter={}", counter));
        assert(counter == 20000);
    }
}
